﻿<?php 
/**
 * 球队的缩写不对应。
 * 意甲是对的，但不完整
 * 
 * 西已在旧版中没有排名
 */
//echo get_content("http://www.okooo.com/league/1/8-3502/schedule/1-36-38/");exit;
set_time_limit(0);ini_set('max_execution_time', 0);
//error_reporting(0);//屏蔽所有错误
$s8 = microtime(true);
ini_set('date.timezone', "Asia/Shanghai");
$fp = fopen("status.txt", "a");//将当前的状态写入文件

include_once 'html.php';
include_once 'team.php';
include_once 'function.php';

$all_m = array(
/*	array(38,"http://www.okooo.com/league/1/182-2720/schedule/1-19-","faYi_10-11","",""),//法乙_10-11
	array(38,"http://www.okooo.com/league/1/182-3381/schedule/1-19-","faYi_11-12","",""),//法乙_11-12
	array(42,"http://www.okooo.com/soccer/league/54/schedule/3503/1-37-","xiYi_11-12","",""),//西乙_11-12
	array(42,"http://www.okooo.com/soccer/league/54/schedule/2897/1-37-","xiYi_10-11","",""),//西乙_10-11
	array(42,"http://www.okooo.com/soccer/league/54/schedule/11003/1-37-","xiYi_12-13","",""),//西乙_12-13
	array(34,"http://www.okooo.com/league/1/44-2812/schedule/1-41-","deYi_10-11","",""),//德乙_10-11
	array(34,"http://www.okooo.com/soccer/league/44/schedule/10940/1-41-","deYi_12-13","",""),//德乙_12-13
	array(42,"http://www.okooo.com/league/1/53-3667/schedule/1-34-","yiYi_11-12","","")//意乙_11-12
	array(42,"http://www.okooo.com/soccer/league/53/schedule/11079/1-34-","yiyi_12-13","",""),//意乙_12-13
	array(42,"http://www.okooo.com/soccer/league/53/schedule/2957/1-34-","yiyi_10-11","",""),//意乙_10-11
	array(42,"http://www.okooo.com/soccer/league/53/schedule/3667/1-34-","yiyi_11-12","",""),//意乙_11-12
	array(38,"http://www.okooo.com/soccer/league/182/schedule/10887/1-19-","fayi_12-13","",""),//法已_12-13
	array(38,"http://www.okooo.com/soccer/league/182/schedule/3381/1-19-","fayi_11-12","",""),//法已_11-12
	array(38,"http://www.okooo.com/soccer/league/182/schedule/2720/1-19-","fayi_10-11","",""),//法已_10-11
	array(38,"http://www.okooo.com/soccer/league/17/schedule/3391/1-1-","yingchao_11-12","",""),//英超_11-12
	array(38,"http://www.okooo.com/soccer/league/17/schedule/2746/1-1-","yingchao_10-11","",""),//英超_10-11
	array(38,"http://www.okooo.com/soccer/league/17/schedule/10912/1-1-","yingchao_12-13","",""),//英超_12-13
	array(38,"http://www.okooo.com/soccer/league/23/schedule/3639/1-33-","yijia_11-12","",""),//意甲_11-12
	array(38,"http://www.okooo.com/soccer/league/23/schedule/2930/1-33-","yijia_10-11","",""),//意甲_10-11
	array(34,"http://www.okooo.com/soccer/league/35/schedule/3405/1-42-","dejia_11-12","",""),//德甲_11-12
	array(34,"http://www.okooo.com/soccer/league/35/schedule/2811/1-42-","dejia_10-11","",""),//德甲_10-11
	array(38,"http://www.okooo.com/soccer/league/34/schedule/3380/1-4-","fajia_11-12","",""),//法甲_11-12
	array(38,"http://www.okooo.com/soccer/league/34/schedule/2719/1-4-","fajia_10-11","",""),//法甲_10-11
	array(38,"http:www.okooo.com/soccer/league/182/schedule/3381/1-19-","fayi_11-12","",""),//法已_11-12
	array(38,"http://www.okooo.com/soccer/league/182/schedule/10887/1-19-","fayi_12-13","",""),//法已_12-13
	array(1,"http://www.okooo.com/zucai/ren9/13043/","13043","2",""),
	array(1,"http://www.okooo.com/zucai/ren9/13044/","13044","2",""),
	*/
	array(34,"http://www.okooo.com/soccer/league/44/schedule/10940/1-41-","deYi_12-13","",""),//德乙_12-13
	);
//--------------------------程序开始------------------------------------------------------------------------------------------
foreach ($all_m as $value55) {
//从终止的地方开始
/* if($value55[1] == "http://www.okooo.com/league/1/54-2253/schedule/1-37-")
	$value55[0] = 6; */
	
$biShai = 0;//每3场写入一个word
$biShai_word = 3;
$html = null;
$temp = get_max_match($value55[2]);
$start_lun = $temp["start_lun"];//从第几轮开始
$start_chang = $temp["start_chang"]+1;//从第第轮结束
//for ($i = 10; $i > 9; $i--) {
for ($i = $start_lun; $i <=$value55[0]; $i++) {
//if($i == 7)break;

	if($value55[3] == "2"){
		$link_array = get_qi_url_id($value55[1], $fp);
		$biShai_word = 1;
	}else{
		$href = $value55[1].$i;
		$link_array = get_lun_url_id($href, $fp);
	}
	
//	print_r($link_array);exit;
	//现在已经得到了每场比赛的id了.正常是10个.在$link_array数组里
	if(empty($link_array)){
		fwrite($fp, "处理后得到每个比赛的链接为空:{$href}\r\n");
		exit;
	}
	//--------------------------正常是10个比赛的id--------------------------------------------------------------------------
	//循环每一场比赛
	$count = 0;
	foreach ($link_array as $key_link => $value) {
		if(empty($value))continue;//有的比赛会推迟，得到的会是0,跳过.http://www.okooo.com/league/1/182-3381/schedule/1-19-23/
		$count++;
		if($i == $start_lun && $count < $start_chang)continue;
		$Europe = get_content("http://www.okooo.com/soccer/match/".$value."/odds/");//欧洲指数
		$company_index = get_company_index($Europe);//到得各个公司的指数
		fwrite($fp, date("Y-m-d H:i:s").",第{$i}轮,{$key_link}场\r\n");
		
		//if(!isGt225($company_index))//99家平均,最后是否大于2.25
			//continue;
		$yaZhouBody = get_content("http://www.okooo.com/soccer/match/{$value}/ah/");//亚洲盘口
		$yaZhou = get_yaZhou_Pankou($yaZhouBody);//到得各个公司的指数
		$href_one = "http://www.okooo.com/soccer/match/".$value."/history/";
		$body_one = get_content($href_one);
		//print_r($body_one);exit;
		if(empty($body_one)){
			fwrite($fp, "得到单个比赛页面为空:{$href_one}\r\n");
			exit;
		}
		//$top = get_top($body_one);//从旧版得到排名
		$top = get_top_new($body_one);//从新版得到排名
		//print_r($top);exit;
	//---处理前10场比赛------------------------------------------------------------	
		//可能会得到70场比赛,我们只要取前10场比赛,因为原页面是由javascript变为10场的
		preg_match('/class="homecomp">(.+)<\/table>/s', $body_one, $body_one_1);
		//得到每一场的tr
		preg_match_all('/<tr(.+?)<\/tr>/s', $body_one_1[1], $arr_one);
		//print_r($arr_one);exit;
		
		if(empty($arr_one[1])){
			fwrite($fp, "处理前10场比赛为空:{$href_one},{$value55[2]},{$i}_{$count}\r\n");
		}else{
			//得到第一列中的日期和西甲
			$td_one = get_td_one($arr_one[1][2], $fp);
			
			$zhuChang_array = Zhuchang($body_one);
			//print_r($zhuChang_array);exit;
			//如果比分为空,则不抓
			$temp1 = strip_tags($zhuChang_array["bifen"]);
			if(empty($temp1))
				continue;
			$zhuChang = getZhuchang($arr_one[1],$fp);
			$key1 = -1;
			$key = 0;
			foreach ($arr_one[1] as $k=>$value) {
				if($k<=1)continue;
				//要选择"非友谊赛"
				if(strpos($value,"data-lt=") === false)continue;
				preg_match('/data-lt="(.*?)"/s', $value, $data_lt);
				if(trim($data_lt[1]) == "friend")continue;
				//if(empty($data_lt) || (trim($data_lt[1]) != "cup" && trim($data_lt[1]) != "league"))
				//	continue;
				$key++;
				if($key >10)
					break;
				$tr = one_tr($value, $fp, $zhuChang);
				//print_r($tr);exit;
				//只要主场的
				if(empty($tr["one"]))
					continue;
				else 
					$key1++;
				//处理排名
				if(trim(strip_tags($tr["one"])) == $zhuChang)
					$tr["one"] .= top_style(top_go($top, trim(strip_tags($tr["two"])), $fp, $team9));
				else
					$tr["two"] .= top_style(top_go($top, trim(strip_tags($tr["one"])), $fp, $team9));
					
				if($key1 == 0){
					//$tr["first"] = $td_one["date"];//将日期放到其中
$tr["first"] = "<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td>{$td_one["date"]}</td><td><div align=right>{$zhuChang_array["bifen"]}</div></td></tr></table>";//将日期放到其中
				}
				if($key1 == 1){
					$tr["first"] = $td_one["name"];//将西甲放到其中
					$tr["first"] .= top_style(top_go($top, trim(strip_tags($zhuChang)), $fp, $team9));
				}
				/*
				if($key1 == 2)
					$tr["first"] = $yaZhou;//将亚洲盘口放到其中
				*/	
				//将欧洲指数,亚洲盘口等放到其中
				$tr["first"] .= insert_company_index($key,$company_index,$yaZhou);
				$html .= html_tr($tr,null);
			}
		}
		//echo $html;exit;
	//---处理后10场比赛--------------------------------------------------------------------------------------------------------	
		//可能会得到70场比赛,我们只要取前10场比赛,因为原页面是由javascript变为10场的
		preg_match('/class="awaycomp"(.+)<\/table>/s', $body_one, $body_one_2);
		//得到每一场的tr
		preg_match_all('/<tr(.+?)<\/tr>/s', $body_one_2[1], $arr_two);
		if(empty($arr_two[1])){
			fwrite($fp, "处理后10场比赛为空:{$href_one},{$value55[2]},{$i}_{$count}\r\n");
		}else{
			$zhuChang = getZhuchang($arr_two[1],$fp);
			$key1 = -1;
			$key = 10;
			foreach ($arr_two[1] as $k=>$value) {
				if($k<=1)continue;
				//要选择"非友谊赛"
				if(strpos($value,"data-lt=") === false)continue;
				preg_match('/data-lt="(.*?)"/s', $value, $data_lt);
				if(trim($data_lt[1]) == "friend")continue;
				//if(empty($data_lt) || (trim($data_lt[1]) != "cup" && trim($data_lt[1]) != "league"))
				//	continue;
				$key++;
				if($key > 20)
					break;
				$tr = one_tr($value, $fp, $zhuChang);
				//只要主场的
				if(empty($tr["one"]))
					continue;
				else
					$key1++;
				//处理排名
				if(trim(strip_tags($tr["one"])) == $zhuChang)
					$tr["one"] .= top_style(top_go($top, trim(strip_tags($tr["two"])), $fp, $team9));
				else
					$tr["two"] .= top_style(top_go($top, trim(strip_tags($tr["one"])), $fp, $team9));
					
				if($key1 == 0)
					$tr["first"] = top_style(top_go($top, trim(strip_tags($zhuChang)), $fp, $team9));//将日期放到其中
				//将欧洲指数,亚洲盘口等放到其中
				$tr["first"] .= insert_company_index($key,$company_index,$yaZhou);
				if($key1 == 0)
					$html .= html_tr($tr,"&nbsp;<span style='color:#0f0'>●</span>");
				else
					$html .= html_tr($tr);
			}
		}
		//echo $html_top.$html.$html_bottom;exit;
		$biShai++;
		if($biShai%$biShai_word == 0){
			if(!file_exists($value55[2]))mkdir($value55[2]);
			$fp8 = fopen($value55[2].DIRECTORY_SEPARATOR."{$i}_{$count}.doc", "a");
			fwrite($fp8, $html_top.$html.$html_bottom);
			$html = "";
			//exit;
			//break;
		}
		//break;
	}
//	break;
}
if(!empty($html)){
	$fp8 = fopen($value55[2].DIRECTORY_SEPARATOR."z.doc", "a");
	fwrite($fp8, $html_top.$html.$html_bottom);
}

}
//--------------------------结束了----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
echo "<br>用时:".number_format(microtime(true) - $s8, 3, '.', '')."秒";